package daily.may0522;

import java.util.*;

public class MapTest {
    public static void main(String[] args) {
        //创建Map集合
        HashMap<String, String> hashMap1 = new HashMap<>();
        //1.put(K key, V value)方法
        //存储键值对的时候,key不重复,返回值V是null
        //存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value值
        String v1 = hashMap1.put("邓超","孙俪");
        System.out.println(v1);//null
        //key不变，value改变
        String v2 = hashMap1.put("邓超","还是孙俪");
        System.out.println(v2);//孙俪，返回的是被替换的value
        System.out.println(hashMap1);//{邓超=还是孙俪}，说明重写了toString方法
        hashMap1.put("冷锋","龙小云");
        hashMap1.put("杨过","小龙女");
        hashMap1.put("YDD","XHH");
        System.out.println(hashMap1);//{邓超=还是孙俪, 杨过=小龙女, YDD=XHH, 冷锋=龙小云}

        //2.remove(Object key)方法，删除键值对，返回被删除的值
        HashMap<String, Integer> hashMap2 = new HashMap<>();
        hashMap2.put("赵丽颖",168);
        hashMap2.put("杨颖",165);
        hashMap2.put("mySoul",162);
        Integer v3 = hashMap2.remove("杨颖");
        System.out.println(v3);//165，返回被删除的value
        Integer v4 =hashMap2.remove("张三李四");
        System.out.println(v4);//null，找不到相应的key返回null

        //3.get(Object key)方法，key存在,返回对应的value值，不存在返回null
        Integer v5 = hashMap2.get("XHH");
        System.out.println(v5);//162，根据key返回对应的value

        //4.containsKey(Object key)方法，判断集合中是否包含指定的键(key)
        boolean v6 = hashMap1.containsKey("邓超");
        System.out.println(v6);//true
        System.out.println(hashMap2.containsKey("冷风"));//false

        //5.containsValue(Object value)方法，判断集合中是否包含指定的值(value)
        System.out.println(hashMap1.containsValue("龙小云"));//true
        System.out.println(hashMap1.containsValue("冷锋"));//false

        //6.keySet()方法，获取Map集合中所有的键，存储到Set集合中并返回该Set集合
        Set<String> set1 = hashMap1.keySet();
        System.out.println(set1);//[邓超, 杨过, YDD, 冷锋]

        //7.values()方法，获取Map集合中所有的值，存储到Collection集合中并返回该Collection集合
        Collection<String> collection = hashMap1.values();
        System.out.println(collection);//[还是孙俪, 小龙女, XHH, 龙小云]

        //8.entrySet()方法：获取Map集合中所有的键值对对象的集合(Set集合)。
        Set<Map.Entry<String, Integer>> set2 = hashMap2.entrySet();
        System.out.println(set2);//[赵丽颖=168, mySoul=162]

        //9.size()方法：返回Map集合键值对映射的个数。]
        System.out.println(hashMap1.size());//4

        //10.remove(Object key,Object value)方法：删除Map集合中键值映射同时匹配的元素,返回是否删除成功
        System.out.println(hashMap1.remove("冷锋","小龙女"));//false
        System.out.println(hashMap1.remove("冷锋","龙小云"));//true
        System.out.println(hashMap1);//{邓超=还是孙俪, 杨过=小龙女, YDD=XHH}

        //11.replace(Object key,Object value)方法：将map集合中指定键对象key所映射的值替换为value，返回被替换的值
        System.out.println(hashMap2.replace("赵丽颖",169));//168
        System.out.println(hashMap2.replace("冷风",169));//null
    }
}
